package com.yookey.common.result;


import com.yookey.common.constant.ResponseMessage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
 * @author wangjian
 * @date 2019/12/17 16:33
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Result<T> {

    /**
     * 请求返回的code
     */
    private int code;

    /**
     * 请求成功返回的数据
     */
    private T data;

    /**
     * 请求返回的消息，不论是请求成功还是请求失败/错误的消息
     */
    private String msg;

    /**
     * 请求返回的status，0：失败，1：成功
     */
    private int status;


    /**
     * 成功时候调用，无返回数据（data）,提示信息自定义
     *
     * @param msg
     * @return
     */
    public static <T> Result<T> success(String msg) {
        Result result = new Result(100,null,msg,1);
        return result;
    }

    /**
     * 成功时候调用，无返回数据（data）
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(ResponseMessage responseMessage) {
        Result result = new Result(responseMessage.getCode(),null,responseMessage.getMsg(),1);
        return result;
    }

    /**
     * 成功时候调用，有返回数据（data）
     *
     * @param data
     * @param responseMessage
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data,ResponseMessage responseMessage) {
        Result result = success(responseMessage);
        result.setData(data);
        return result;
    }

    /**
     * 成功时调用
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data){
        Result result = success(ResponseMessage.SUCCESS);
        result.setData(data);
        return result;
    }
    /**
     * 成功时候调用，有返回数据（data）,提示信息自定义
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data, String msg) {
        Result result = success(msg);
        result.setData(data);
        return result;
    }

    /**
     * 失败时候调用，无返回数据（data）
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(ResponseMessage responseMessage) {
        Result result = new Result(responseMessage.getCode(), null, responseMessage.getMsg(), 0);
        return result;
    }

    public static <T> Result error(String msg) {
        Result result = new Result(114, null, msg, 0);
        return result;
    }

    public Result<T> error500(String message) {
        this.msg = message;
        this.code = 500;
        this.status = 0;
        return this;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> result = new Result<>();
        result.setStatus(1);
        result.setCode(200);
        result.setData(data);
        return result;
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setStatus(0);
        return r;
    }
}
